package com.limf.demo.java8;

import java.text.SimpleDateFormat;
import java.time.Clock;
import java.time.DayOfWeek;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.Locale;
public class ClockTest {

	public static void main(String[] args) {
		//test();
		//timezonesTest();
		//localTimeTest();
		
		// localdateTest();
		// localDateTimeTest();
		parseDateTime();
	}
	
	static void test() {
		Clock clock = Clock.systemDefaultZone();
		long millis = clock.millis();
		Instant instant = clock.instant();
		Date legacyDate = Date.from(instant);
		System.out.println("millis"+ millis);
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		System.out.println("legacyDate:"+sf.format(legacyDate));
	}
	
	static void timezonesTest() {
		System.out.println(ZoneId.getAvailableZoneIds());
		// prints all available timezone ids
		ZoneId zone1 = ZoneId.of("Europe/Berlin");
		ZoneId zone2 = ZoneId.of("Brazil/East");
		System.out.println(zone1.getRules());
		System.out.println(zone2.getRules());
		
		LocalTime now1 = LocalTime.now(zone1);
		LocalTime now2 = LocalTime.now(zone2);
		System.out.println(now1.isBefore(now2));
		long hoursBetween = ChronoUnit.HOURS.between(now1, now2);
		long minutesBetween = ChronoUnit.MINUTES.between(now1, now2);
		System.out.println(hoursBetween);    // 4
		System.out.println(minutesBetween);   // 299
		
	}
	
	static void localTimeTest() {
		
		LocalTime late = LocalTime.of(23, 59, 59);
		System.out.println(late);   // 23:59:59
		DateTimeFormatter germanFormatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT)
				.withLocale(Locale.GERMAN);
		LocalTime leetTime = LocalTime.parse("13:37", germanFormatter);
		System.out.println(leetTime);
	}
	
	static void localdateTest() {
		LocalDate today = LocalDate.now();
		LocalDate tomorrow = today.plus(1, ChronoUnit.DAYS);
		LocalDate yeterday = tomorrow.minusDays(2);
		LocalDate independenceDay = LocalDate.of(2014, Month.JULY, 4);
		DayOfWeek dayOfWeek = independenceDay.getDayOfWeek(); 
		System.out.println("today:"+today);
		System.out.println("tomorrow:"+tomorrow);
		System.out.println("yeterday:"+yeterday);
		System.out.println("independenceDay:"+independenceDay);
		System.out.println("dayOfWeek:"+dayOfWeek);
		
		System.out.println("----------------------");
		
		DateTimeFormatter germanFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)
				.withLocale(Locale.GERMAN);
		LocalDate xmas = LocalDate.parse("24.12.2014", germanFormatter);
		System.out.println(xmas);
	}
	
	static void localDateTimeTest() {
		LocalDateTime sylvester = LocalDateTime.of(2014, Month.DECEMBER, 31, 23, 59, 59);
		DayOfWeek dayOfWeek = sylvester.getDayOfWeek();
		System.out.println(dayOfWeek);     // WEDNESDAY
		Month month = sylvester.getMonth();
		System.out.println(month);    // DECEMBER
		long minuteOfDay = sylvester.getLong(ChronoField.MINUTE_OF_DAY);
		System.out.println(minuteOfDay);
		
		Instant instant = sylvester.atZone(ZoneId.systemDefault()).toInstant();
		Date legacyDate = Date.from(instant);
		System.out.println(legacyDate);
	}
	
	static void parseDateTime() {
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
		LocalDateTime parsed = LocalDateTime.parse("2014-11-03 07:13", formatter);
		String string = formatter.format(parsed);
		System.out.println(string);   // Nov 03, 2014 - 07:13
	}
	
}
